package cn.example.vip.mall.goods.service.impl;

import cn.example.vip.mall.goods.entity.CategoryAttr;
import cn.example.vip.mall.goods.mapper.CategoryAttrMapper;
import cn.example.vip.mall.goods.service.CategoryAttrService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryAttrServiceImpl extends ServiceImpl<CategoryAttrMapper, CategoryAttr> implements CategoryAttrService {
	@Autowired
	CategoryAttrMapper categoryAttrMapper;
	
	/**
	 * 条件查询
	 * @param categoryAttr
	 * @return
	 */
	@Override
	public List<CategoryAttr> queryList(CategoryAttr categoryAttr) {
		QueryWrapper<CategoryAttr> wrapper = new QueryWrapper<>();
		wrapper.like(categoryAttr.getCategoryId() != null, "category_id", categoryAttr.getCategoryId());
		return categoryAttrMapper.selectList(wrapper);
	}
	
	/**
	 * 分页查询
	 * @param categoryAttr
	 * @param page
	 * @param size
	 * @return
	 */
	@Override
	public Page<CategoryAttr> queryPageList(CategoryAttr categoryAttr, Long page, Long size) {
		QueryWrapper<CategoryAttr> wrapper = new QueryWrapper<>();
		wrapper.like(categoryAttr.getCategoryId() != null, "category_id", categoryAttr.getCategoryId());
		return categoryAttrMapper.selectPage(new Page<>(page, size), wrapper);
	}
	
	@Override
	public Integer updateByCategory(CategoryAttr categoryAttr, Integer categoryId, Integer attrId) {
		QueryWrapper<CategoryAttr> wrapper = new QueryWrapper<>();
		wrapper.eq(categoryId != null, "category_id", categoryId);
		wrapper.eq(attrId != null, "attr_id", attrId);
		return categoryAttrMapper.update(categoryAttr, wrapper);
	}
	
	@Override
	public Integer deleteById(Integer categoryId, Integer attrId) {
		QueryWrapper<CategoryAttr> wrapper = new QueryWrapper<>();
		wrapper.eq(categoryId != null, "category_id", categoryId);
		wrapper.eq(attrId != null, "attr_id", attrId);
		return categoryAttrMapper.delete(wrapper);
	}
}
